#include<iostream>
#include<set>
#include"Inetaddr.hpp"
#include"Log.hpp"

using namespace LogMod;

class Task{
    bool exist(const Inetaddr& addr)
    {
        auto iter = _set.find(addr);
        return iter != _set.end();
    }

    void pushaddr(const Inetaddr& addr)
    {
        _set.insert(addr);
        LOG(level::INFO)<< addr.String_addr()<< " inline";
    }

public:
    Task(){}
    

    void route(const int& sfd,const Inetaddr& addr,const std::string& str)
    {
        SimpleMutex mutex(_mutex);
         if(!exist(addr))
         {
            std::cout<<exist(addr)<<std::endl;
            pushaddr(addr);
        }
        
        //std::cout<<addr.String_addr()<<" say# ";

        for(auto& e :_set)
        {
            //std::cout<<"test sendto"<<std::endl;
            ssize_t n = sendto(sfd,str.c_str(),str.size(),0,(const struct sockaddr*)&addr.Getaddr(),sizeof(addr.Getaddr()));
            if(n < 0)
            {
                LOG(level::ERROR);
                exit(2);
            }
        }

        // char buff[512];
        // for(auto& e :_set)
        // {
        //     socklen_t len = e.Lenaddr();
        //     recvfrom(sfd,buff,sizeof(buff),0,e.GetaddrPtr(),&len);
        // }

    }

    ~Task(){}
private:
    std::set<Inetaddr> _set;
    Mutex _mutex;
};